package cn.lix.sc.other.test;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Arrays;

public class PdfUtils {

    public static final String Encoding = "utf-8";

    /**
     * HTML 转 PDF
     * @param content html内容
     * @param outPath           输出pdf路径
     * @return 是否创建成功
     */
    public static boolean html2Pdf(String content, String outPath) {
        try {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPath));
            document.open();
            XMLWorkerHelper.getInstance().parseXHtml(writer,
                    document,
                    new ByteArrayInputStream(content.getBytes()),
                    XMLWorkerHelper.class.getResourceAsStream("/default.css"),
                    Charset.forName(Encoding),
                    new CustomXMLWorkerFontProvider());
            document.close();
        } catch (Exception e) {
            System.out.println("生成模板内容失败"+e.fillInStackTrace());
            return false;
        }
        return true;
    }
    /**
     * HTML 转 PDF
     * @param content html内容
     * @return PDF字节数组
     */
    public static byte[] html2Pdf(String content) {
        ByteArrayOutputStream outputStream = null;
        try {
            Document document = new Document();
            outputStream = new ByteArrayOutputStream();
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                    new ByteArrayInputStream(content.getBytes(Encoding)), null, Charset.forName(Encoding));
            document.close();
        } catch (Exception e) {
            System.out.println("------生成pdf失败-------");
        }
        return outputStream.toByteArray();
    }


    public static void createPdf() {
        AgreementMasterDto masterDto = new AgreementMasterDto();
        masterDto.setAgreementNo("ZZCLZLSFXY20220527000040");
        masterDto.setSettleDate(22);
        masterDto.setRentOrgName("测试项目部");
        masterDto.setRentoutOrgName("成都公司租赁中心");
        masterDto.setThirdTypeName("现代物流");
        masterDto.setRentManagerName("承组方");
        masterDto.setRentManagerId("510130199011224554");
        masterDto.setRentManagerPhone("18080808887");
        masterDto.setRentoutManagerName("出租方");
        masterDto.setRentoutManagerId("110160198901236654");
        masterDto.setRentoutManagerPhone("15866554125");
        masterDto.setSummation("15520.00000000");
        AgreementDtlDto dtlDto = new AgreementDtlDto();
        dtlDto.setCatalogCode("0000003116562");
        dtlDto.setCatalogName("球墨铸铁");
        dtlDto.setModel("Φ1000防盗型球墨铸铁井盖");
        dtlDto.setUom("套");
        dtlDto.setQty(new BigDecimal(10));
        dtlDto.setNoTaxPrice("50.00000000");
        dtlDto.setRentPeriod(new BigDecimal("30"));
        dtlDto.setLeaseAmount("15000.00000000");
        dtlDto.setMaintenanceFees("20.00000000");
        dtlDto.setCompensateLoss("500.00000000");
        dtlDto.setSubtotal("15520.00000000");
        masterDto.setDtl(Arrays.asList(dtlDto));
        ClassLoader classLoader = PdfUtilTest.class.getClassLoader();
        //获取模板路径
        URL resource = classLoader.getResource("templates");
        try {
            String templateDirectory  =resource.toURI().getPath();
            String templateContent = PdfUtilTest.getTemplateContent(templateDirectory, "pdf_template.ftl", masterDto);
            System.out.println(templateContent);
            PdfUtils.html2Pdf(templateContent, "pdf_template.pdf");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
